<?php
/*
 *      ositem.php
 *      
 *      Copyright 2010 Vitaliy Rakitin <virak@ukr.net>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
 
//require_once($_SERVER['DOCUMENT_ROOT']."/classes/System/osfields.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/System/ostabular.php");
//require_once($_SERVER['DOCUMENT_ROOT']."/classes/DB/DB.php");
//require_once($_SERVER['DOCUMENT_ROOT']."/classes/GUI/nwmsg.php");

class ositem {
	
	private $uuid;
	//private $name;
	private $meta;
	private $fields = array();
	private $tabulars = array();
	private $isedit = false;
	
	function __construct($uuid, $meta) {
		$this->uuid = $uuid;
		$this->meta = $meta;
		foreach ($this->meta->fields->getall() as $metafield) {
			$this->fields[$metafield->uuid] = $metafield->defaultvalue;
		}
		
		foreach ($this->meta->tabulars->getall() as $metatabular) {
			$this->tabulars[$metatabular->uuid] = new ostabular($metatabular);
		}
	}

	function __get($name) {
		foreach ($this->meta->fields->getall() as $metafield) {
			if ($name == $metafield->name) {
				return $this->fields[$metafield->uuid];
			}
		}
		
		foreach ($this->meta->tabulars->getall() as $metatabular) {
			if ($name == $metatabular->name) {
				return $this->tabulars[$metatabular->uuid];
			}
		}
		
		switch ($name) {
			case "uuid": {return $this->uuid; break;}
		}
	}
	
	function __set($name, $value) {
		foreach ($this->meta->fields->getall() as $metafield) {
			if ($name == $metafield->name) {
				$this->fields[$metafield->uuid] = $value;
				return true;
			}
		}
		
	}	
	
	function __sleep() {
		//return $this->fields;
		return array('uuid','fields','meta');
	}
	
	function save() {
		// Записать данные в базу
	}
	
	function open($data) {
		
	}
}
?>

